// pages/loupan/detail.js
var app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		id: '',
		detail: '',
		moretext: "点击查看更多",
		showmore: false,
		liked: false,
		imglist: '',
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		console.log(options);
		var that = this;
		that.setData({
			id: options.id
		})
		that.detail();
	},
	detail() {
    wx.showLoading({
      title: '加载中',
    })
		var id = this.data.id;
		var that = this;
		var opid = wx.getStorageSync('user').openid;
		app.core('get', 'get_oldhouse_detail/id/' + id + '', {}
			, (res) => {

        var arr =res.data;
   
          switch (arr.seeHouseTime) {
            case 1:
              arr.seeHouseTime = '随时看房'
              break;
            case 2:
              arr.seeHouseTime = '非工作时间看房'
              break;
            case 3:
              arr.seeHouseTime = '预约看房'
              break;
           
          }
       
        console.log(arr,122)
				that.setData({
					detail:arr
				})
        wx.hideLoading()
			}
		)
	},// 获取详情
	btnliked() {

		var that = this;
		var liked = that.data.liked;
		if (liked == false) {
			that.liked()
		} else {
			that.dislike()
		}
	},
	liked() {
    wx.showLoading({
      title: '加载中',
    })
		var opid = wx.getStorageSync('user').openid;
		var id = this.data.id;
		var liked = this.data.liked;

		app.core('get', 'create_user_collect/open_id/' + opid + '/type/1/lid/' + id + '', '', (res => {
			this.setData({
				liked: true,
			})
			console.log(this.data.liked)
			wx.showToast({
				title: '收藏成功',
				icon: 'none',
				duration: 1000
			})
      wx.hideLoading()
		}))


	},// 收藏
	dislike() {
    wx.showLoading({
      title: '加载中',
    })
		var that = this;
		var id = that.data.id;
		var like = that.data.liked;
		var opid = wx.getStorageSync('user').openid;

		app.core('get', 'cancel_user_collect/open_id/' + opid + '/type/2/lid/' + id + '', "", (res => {
			console.log(res)
			wx.showToast({
				title: '取消收藏',
				icon: 'none',
				duration: 1000
			})
			that.setData({
				liked: false
			})
      wx.hideLoading()
		}))


	},// 取消收藏

	previewImage(e) {
		var that =this;
		var current = e.target.dataset.src;
		var arr=[];
		var x = that.data.detail.oldhouse_photo;
		for(var i=0;i<x.length;i++){
			arr.push(x[i].photoUrl)
		}
		wx.previewImage({
			current: current, // 当前显示图片的http链接  
			urls: arr
		})

	},//预览图片
	addmore() {
		var show = this.data.showmore;

		if (show == true) {
			console.log(show)
			this.setData({
				showmore: false,
				moretext: '点击查看更多'
			})
		}
		else if (show == false) {
			console.log(show)
			this.setData({
				showmore: true,
				moretext: '点击收起'
			})
		}

	},
	// 点击加载更多
	share() {
		wx.updateShareMenu({
			withShareTicket: true,
			success(res) {
				console.log(res)
			}
		})
	},
	// 分享转发
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享 
	 */
	onShareAppMessage: function (res) {
		return {
			title: this.data.detail.title,
			path: 'pages/loupan/ershoudetail?id=' + this.data.id + ''
		}
	}
})